<?php
namespace user;
use View;
use BaseController;
use Auth;
use User;
use Blog;
use Input;
use Validator;
use Redirect;
use DB;
use BlogUserCategory;
use BlogSystemCategory;

class BlogController extends BaseController {

	/**
	 * 博文列表
	 * @return [type] [description]
	 */
	public function index()
	{
		//
	}

	/**
	 * 发布博文
	 * @return [type] [description]
	 */
	public function addblog(){

		return View::make('user.blog.addblog')->with('title','发布博文')->with('user_category',get_user_category(Auth::id()));
	}

	/**
	 * 修理发布博文操作
	 * @return [type] [description]
	 */
	public function doAddBlog(){
		$rules = array(
			'title' => 'required|min:5',
			'content' => 'required|min:10'
		);

		$validator = Validator::make(Input::all(),$rules);
		if($validator->passes()){
			$id = Input::get('id');
			if($id){

			}else{
				$blog = Blog::create(Input::all());
				$blog->user_id = Auth::id();
			}

			if($blog->save()){
				Redirect::to('user/blog')->with('message',array('type'=>'success','content'=>'操作成功'));
			}else{
				Redirect::back()->with('message',array('type'=>'danger','content'=>'操作失败'))->withInput();
			}
		}else{
			Redirect::back()->withInput()->withErrors($validator);
		}
	}

	/**
	 * 博客分类首页
	 * @param  integer $id [description]
	 * @return [type]      [description]
	 */
	public function category($id = 0){
		//有ID则修改，没有则新增
		if($id){
			$sub_title = '修改博客分类';
			$one = BlogUserCategory::find($id);
		}else{
			$sub_title = '添加博客分类';
			$one = '';
		}

		//全部分类列表
		$categories = BlogUserCategory::where('user_id',Auth::id())->get();

		return View::make('user.blog.category')->with('title','博客管理')->with('sub_title',$sub_title)->with('one',$one)->with('categories',$categories);
	}

	/**
	 * 处理添加和修改博客分类操作
	 * @return [type] [description]
	 */
	public function doCategory(){
		$rules = array(
			'name' => 'required|min:4',
			'parent_id' => 'required|integer',
			'sort_order' => 'integer'
		);

		$validator = Validator::make(Input::all(),$rules);
		if($validator->passes()){
			//判断是否存在id，存在则为修改
			$id = Input::get('id');
			if($id){
				$category = BlogUserCategory::find($id);
				$category->name = Input::get('name');
				$category->parent_id = Input::get('parent_id');
				$category->sort_order = Input::get('sort_order');
			}else{
				$category = BlogUserCategory::create(Input::only('name','parent_id','sort_order'));
				$category->user_id = Auth::id();
			}

			if($category->save()){
				return Redirect::back()->with('message',array('type'=>'success','content'=>'操作成功'));
			}else{
				return Redirect::back()->with('message',array('type'=>'danger','content'=>'操作成功 '))->withInput();
			}
		}else{
			return Redirect::back()->withInput()->withErrors($validator);
		}
	}

	public function delCategory($id){
		$category = BlogUserCategory::find($id);
		if($category && $category->delete()){
			return Redirect::back()->with('message',array('type'=>'success','content'=>'删除成功'));
		}else{
			return Redirect::back()->with('message',array('type'=>'danger','content'=>'删除失败'));
		}
	}

	/**
	 * Show the form for creating a new resource.
	 * GET /user/blog/create
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 * POST /user/blog
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 * GET /user/blog/{id}
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 * GET /user/blog/{id}/edit
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 * PUT /user/blog/{id}
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 * DELETE /user/blog/{id}
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}

}